﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.OutboundManage
{
    public partial class VoiceList : Common.PageBase
    {
        string temp = string.Empty;
        public string ModifyUrl = "VoiceModify.aspx?ID={0}";
        T_VOICEMAIL_BLL bll = new T_VOICEMAIL_BLL();
        //SqlServerDAL<T_VOICEMAIL_Entity> dal = new SqlServerDAL<T_VOICEMAIL_Entity>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetPopCalender(this.Page, txtALLOCATEDTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtALLOCATEDTIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtSTARTTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtSTARTTIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtCallBackCompleteTimeBegin);
            PageHelper.SetPopCalender(this.Page, txtCallBackCompleteTimeEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlSTATUSBinding();

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }


        #region 页面初始化数据绑定




        private void ddlSTATUSBinding()
        {
            EnumHelper.FillListControl(ddlSTATUS, typeof(VoiceStatus));
            ddlSTATUS.Items.Insert(0, new ListItem("", ""));
        }





        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                sql.Append(" and ID like '%" + Request.QueryString["ID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DNIS"]))
            {
                sql.Append(" and DNIS like '%" + Request.QueryString["DNIS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ANI"]))
            {
                sql.Append(" and ANI like '%" + Request.QueryString["ANI"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STARTTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("STARTTIME", Request.QueryString["STARTTIMEBegin"].Trim()));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STARTTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("STARTTIME", Request.QueryString["STARTTIMEEnd"].Trim()));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENDTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("ENDTIME", Request.QueryString["ENDTIMEBegin"].Trim()));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENDTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("ENDTIME", Request.QueryString["ENDTIMEEnd"].Trim()));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CallBackCompleteTimeBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CallBackCompleteTime", Request.QueryString["CallBackCompleteTimeBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CallBackCompleteTimeEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CallBackCompleteTime", Request.QueryString["CallBackCompleteTimeEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DOCUMENTPATH"]))
            {
                sql.Append(" and DOCUMENTPATH like '%" + Request.QueryString["DOCUMENTPATH"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LENGTH"]))
            {
                sql.Append(" and LENGTH >= '" + Request.QueryString["LENGTH"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LENGTHMIN"]))
            {
                sql.Append(" and LENGTH < '" + Request.QueryString["LENGTHMIN"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ASSIGNEDPERSONID"]))
            {
                sql.Append(" and ASSIGNEDPERSONID = '" + Request.QueryString["ASSIGNEDPERSONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ASSIGNEDPERSONNAME"]))
            {
                sql.Append(" and ASSIGNEDPERSONNAME like '%" + Request.QueryString["ASSIGNEDPERSONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DISTRIBUTIONPEOPLEID"]))
            {
                sql.Append(" and DISTRIBUTIONPEOPLEID = '" + Request.QueryString["DISTRIBUTIONPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DISTRIBUTIONPEOPLENAME"]))
            {
                sql.Append(" and DISTRIBUTIONPEOPLENAME like '%" + Request.QueryString["DISTRIBUTIONPEOPLENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ALLOCATEDTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("ALLOCATEDTIME", Request.QueryString["ALLOCATEDTIMEBegin"].Trim()));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ALLOCATEDTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("ALLOCATEDTIME", Request.QueryString["ALLOCATEDTIMEEnd"].Trim()));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LIMITED"]))
            {
                sql.Append(" and LIMITED like '%" + Request.QueryString["LIMITED"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CASEID"]))
            {
                sql.Append(" and CASEID = '" + Request.QueryString["CASEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CALLTYPE"]))
            {
                sql.Append(" and CALLTYPE like '%" + Request.QueryString["CALLTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '" + Request.QueryString["BUSINESSDIVISIONCODE"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CompleteLength"]))
            {
                sql.Append(" and to_number(CallBackCompleteTime - STARTTIME)*24  <= '" + Request.QueryString["CompleteLength"] + "'");
            }

            if(!UserLoginInfo.IsAdministrator())
            {
                sql.Append(String.Format(" and DNIS like '{0}%' ", UserLoginInfo.GetLoginInfo().BusinessdivisionEntity.VIRTUALGROUP));
            }

            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_VOICEMAIL_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "ID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_VOICEMAIL_Entity entity = new T_VOICEMAIL_Entity();
            entity.ID = (sender as GridView).DataKeys[e.RowIndex].Values[0].ToString();

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "STATUS":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            if (temp.Trim() == ((int)VoiceStatus.待回电).ToString())
                                            {
                                                e.Row.Attributes["style"] = "color:green;";
                                            }
                                            if (temp.Trim() == ((int)VoiceStatus.未接通).ToString())
                                            {
                                                e.Row.Attributes["style"] = "color: blue;";
                                            }
                                            temp = ((VoiceStatus)Enum.Parse(typeof(VoiceStatus), temp)).ToString();
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
